package cn.crc.pjblog.pages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.Messages;

import cn.crc.model.BlogCategory;
import cn.crc.model.BlogMember;
import cn.crc.model.dao.PublicDAO;
import cn.crc.model.imp.IPublicDAO;
import cn.crc.pjblog.services.RedirectException;
import cn.crc.spring.SpringBean;

public class BlogPostOne {
	
	@ApplicationState	
	private BlogMember loginBlogMember;	
	private boolean loginBlogMemberExists;
	
	@Inject
	@SpringBean("IPublicDAO")
	private IPublicDAO pdao;
	
	@Component
	private Form frmQuery;

	//for selectValueSame component
	
	@Persist("flash")
	private BlogCategory blogCategoryBean;
	private List<BlogCategory> blogCategoryList;
	
	@InjectPage
	private BlogPostTwo  blogPostTwo;
	
	
	public BlogCategory getBlogCategoryBean() {
		return blogCategoryBean;
	}
	public List<BlogCategory> getBlogCategoryList() {
		return blogCategoryList;
	}
	public void setBlogCategoryBean(BlogCategory blogCategoryBean) {
		this.blogCategoryBean = blogCategoryBean;
	}
	public void setBlogCategoryList(List<BlogCategory> blogCategoryList) {
		this.blogCategoryList = blogCategoryList;
	}
	
//	public PublicDAO getPdao() {
//		return pdao;
//	}
//	public void setPdao(PublicDAO pdao) {
//		this.pdao = pdao;
//	}
	/**
	 * 做一些页面的准备工作，
	 * 1、从数据库中取出来blog的日志分类类型
	 * */
	public void pageLoaded(){
		
		System.out.println("loginBLogMemberExists=="+loginBlogMemberExists);
		
//		System.out.println("pageLoaded=="+ (order++));
		String hsql="from BlogCategory";
		List<BlogCategory> list = new ArrayList<BlogCategory>();
		List listOne = this.pdao.getDataByHSQL(hsql);
//		System.out.println("listOne.size="+listOne.size());
//		list.add(new BlogCategory());
		for (Iterator it=listOne.iterator();it.hasNext();)
		{
			BlogCategory bc =(BlogCategory) it.next();
			
//			System.out.println("bc.mmm=="+bc.getCateName());
			list.add(bc);
		}
//		System.out.println("list.size=="+list.size());
		this.setBlogCategoryList(list);
//		this.setBlogCategoryBean(new BlogCategory());
	}
	/***
	 * @author doublel
	 * 
	 *发表Blog文章的时候选择类别和编辑器后提交到下一个页面BlogPostTwo
	 *BlogPostTwo显示用户录入界面
	 * @return BlogPostTwo
	 * */
	public Object onActionFromfrmQuery(){
		
		BlogCategory  bc = this.getBlogCategoryBean();
//		System.out.println("bc.name=="+bc.getCateName());
		this.blogPostTwo.setPreBlogCategory(bc);
		if (this.frmQuery.isValid()) return "BlogPostTwo";
		
		return null;
	}
	/***
	 * 页面在renderder之前执行的方法
	 * 在这里进行用户验证如果没有登录系统就
	 * 不允许发表Blog
	 * 
	 * */
	void onActivate(){
		
//		System.out.println("onActivate=="+ (order++));
//		用户没有登录需要抛出未登录异常
		if(this.loginBlogMemberExists == false)
		{
		
			throw new RedirectException("exceptions/InvalidUserException");
		}
		
		
		
				
	}
	/**
	 * 
	 * 
	 * */
	BlogCategory onPassivate(){
		
//		System.out.println("onPassivate=="+ (order++));
//		System.out.println("onPassivate22222=="+ this.getBlogCategoryBean().getCateName());
//		
		return this.getBlogCategoryBean();
	}
	
	
	void pageAttached(){
//		System.out.println("pageAttached=="+ (order++));	
		
	}
	
	
	void pageDetached(){
//		System.out.println("pageDetached=="+ (order++));	
		
	}
	
		
	/**
	 * @param loginBlogMember the loginBlogMember to set
	 */
	public void setLoginBlogMember(BlogMember loginBlogMember) {
		this.loginBlogMember = loginBlogMember;
	}
	
}
